package com.ruoyi.project.medicinal.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.medicinal.domain.StockMedicines;
import com.ruoyi.project.medicinal.domain.StockProducter;
import com.ruoyi.project.medicinal.domain.SyshisDictData;
import com.ruoyi.project.medicinal.service.MedicinesService;
import com.ruoyi.project.medicinal.service.SyshisDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/med/med")
public class MedicinesController extends BaseController {

    @Autowired
    private MedicinesService medicinesService;

    @Autowired
    private SyshisDictDataService syshisDictDataService;

    /**
     * 获取药品列表
     */
    @PreAuthorize("@ss.hasPermi('med:med:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockMedicines medicines) {
        startPage();
        List<StockMedicines> list = medicinesService.selectPostList(medicines);
        return getDataTable(list);
    }

    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('med:med:add')")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StockMedicines medicines) {
        medicines.setCreateBy(SecurityUtils.getUsername());
        return toAjax(medicinesService.insertMed(medicines));
    }

    /**
     * 根据药物编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('med:med:query')")
    @GetMapping(value = "/{medicinesId}")
    public AjaxResult getInfo(@PathVariable Long medicinesId) {
        return AjaxResult.success(medicinesService.selectMedById(medicinesId));
    }

    /**
     * 修改药物
     */
    @PreAuthorize("@ss.hasPermi('med:med:edit')")
    @Log(title = "药物管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StockMedicines medicines) {
        medicines.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(medicinesService.updateMed(medicines));
    }

    /**
     * 批量删除药物
     */
    @PreAuthorize("@ss.hasPermi('med:med:remove')")
    @Log(title = "药物管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{medicinesId}")
    public AjaxResult remove(@PathVariable Long[] medicinesId) {
        return toAjax(medicinesService.deleteMedByIds(medicinesId));
    }


    /**
     * 获取所有药品
     */
    @PreAuthorize("@ss.hasPermi('med:med:list')")
    @GetMapping("/getMed")
    public AjaxResult getMed(StockMedicines medicines) {
        List<SyshisDictData> list = syshisDictDataService.selectMed();
        return AjaxResult.success(list);
    }
}
